<template>
    <div class="divKms">
        <div class="divArea">
            <div class="divHead">
                <div class="divTitle">channel</div>
                <ul class="lstAcs">
                    <li>
                        <el-button @click="createChannel" size="mini"><i class="fa fa-plus"></i></el-button>
                    </li> 
                    <li>
                        <el-button @click="refreshChannels" size="mini"><i class="fa fa-refresh"></i></el-button>
                    </li> 
                </ul>
            </div>
            <div class="divBody">
                <div class="divSec"
                    v-for= "(ch,chIndex) in channels"
                    :key="chIndex"                    
                >
                    <div class="divSecHead">
                        <div class="divTitle">{{ch.channelCode}}</div>
                        <ul class="lstAcs">
                            <el-button type="text" @click="createSeat" :data-channel="ch.channelCode" size="mini"><i class="fa fa-plus"></i></el-button>
                            <el-button type="text" @click="closeChannel" :data-channel="ch.channelCode" size="mini"><i class="fa fa-close"></i></el-button>
                        </ul>
                    </div>
                    <div class="divSecBody">
                        <ul class="lstSeats">
                            <li
                                v-for= "(seat,seatIndex) in Object.values(ch.seats)"
                                :key="seatIndex"   
                                
                            >
                                <div class="divSeatCode">{{seat.seatCode}}</div>
                                <div class="acs">
                                    <el-button @click="closeSeat" :data-channel="ch.channelCode" :data-seat="seat.seatCode" size="mini" type="text"><i class="fa fa-close"></i></el-button>
                                </div>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
        <div class="divArea">
            <div class="divHead hidden" >
                <div class="divTitle">device</div>
                <ul class="lstAcs">
                    <li>
                        <el-select v-model="currentDeviceId" placeholder="请选择" size="mini">
                            <el-option
                            v-for="(item,index) in devices"
                            :key="index"
                            :label="item.label"
                            :value="item.deviceId">
                            </el-option>
                        </el-select>
                    </li>  
                </ul>
                
            </div>
            <div class="divBody"> 
                <div class="divSec"
                    v-for= "(peer,peerIndex) in devices"
                    :key="peerIndex"                    
                >
                    <div class="divSecHead">
                        <div class="divTitle">{{peer.label}}</div>
                        <ul class="lstAcs">
                            <el-button type="text" @click="createSeat" :data-channel="peer.frviceId" size="mini"><i class="fa fa-plus"></i></el-button>
                            <el-button type="text" @click="closeChannel" :data-channel="peer.frviceId" size="mini"><i class="fa fa-close"></i></el-button>
                        </ul>
                    </div>
                    <div class="divSecBody" style="padding:0"> 
                        <div class="divLocalVideo">
                            <video :id="'lv-'+peerIndex" muted autoplay></video>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="divMain">
            <div class="divRvs">
                <div class="divRemoteVideo">
                    <video muted autoplay></video>
                </div>
                <div class="divRemoteVideo">
                    <video muted autoplay></video>
                </div>
                <div class="divRemoteVideo">
                    <video muted autoplay></video>
                </div>
                <div class="divRemoteVideo">
                    <video muted autoplay></video>
                </div>
                <div class="divRemoteVideo">
                    <video muted autoplay></video>
                </div>
                <div class="divRemoteVideo">
                    <video muted autoplay></video>
                </div>
                <div class="divRemoteVideo">
                    <video muted autoplay></video>
                </div>
                <div class="divRemoteVideo">
                    <video muted autoplay></video>
                </div>
                <div class="divRemoteVideo">
                    <video muted autoplay></video>
                </div>
            </div>
        </div>
 
    </div>
</template>

<script>
    var kms = require('./kmsClient.js');
    export default {
        mounted(){
            var me = this;
            try{
                me.init();
            }
            catch(er){
                console.log(er);
            }
        },
        data(){
            return{
                channelCode:"hhRtc",
                channels:[],
                devices:[],
                currentDeviceId:"",
                pushers:{},
                currentSeat:{
                    showDlg:false,
                    seats:[],
                    seat:""
                }
            }
        },
        methods:{
            init(){
                var me = this;
                try{
                    me.kms.getKmsClient().then(_=>{
                        console.log('connect to kms ...');
                        return me.kms.getDevices();
                    }).then(_devs=>{
                        me.devices = _devs.videoinput;
                        me.refreshChannels(); 
                    });
                }
                catch(er){
                    console.log(er);
                }
            },
            refreshChannels(){
                var me = this;
                try{
                    me.kms.getChannels( ).then(_chs=>{
                        console.log(_chs); 
                        me.channels = _chs.data.filter(ch=>{
                            return ch.objName && ch.channelCode && ch.channelCode==ch.objName;
                        });
                        me.refreshLocalPeers();
                        me.refreshCurrentSeats();
                    })
                }
                catch(er){
                    console.log(er);
                }
            },
            createChannel(){
                var me = this;
                try{
                    
                    this.$prompt('please input channel code', 'channel', {
                        confirmButtonText: 'OK',
                        cancelButtonText: 'Cancel' 
                    }).then(({ value }) => {
                        me.channelCode = value;
                        me.kms.createChannel(me.channelCode).then(_ch=>{
                            console.log(_ch);
                            me.refreshChannels();
                        })
                    }).catch(() => { 
                        ;
                    }); 

                }
                catch(er){
                    console.log(er);
                }
            },
            closeChannel(e){
                var me = this;
                try{
                    var chCode = e.currentTarget.dataset.channel;
                    me.kms.closeChannel(chCode).then(_ch=>{
                        console.log(_ch);
                        me.refreshChannels();
                    })
                }
                catch(er){
                    console.log(er);
                }
            },
            createSeat(e){
                var me = this;
                try{
                    var chCode = e.currentTarget.dataset.channel;
                    this.$prompt('please input seat code', 'seat', {
                        confirmButtonText: 'OK',
                        cancelButtonText: 'Cancel' 
                    }).then(({ value }) => {
                        var seatCode = value;
                        me.kms.createSeat(chCode , seatCode).then(_seat=>{
                            console.log(_seat);
                            me.refreshChannels();
                        })
                    }).catch(() => { 
                        ;
                    }); 

                }
                catch(er){
                    console.log(er);
                }
            },
            closeSeat(e){
                var me = this;
                try{
                    var chCode = e.currentTarget.dataset.channel;
                    var seatCode = e.currentTarget.dataset.seat;
                    me.kms.closeSeat(chCode, seatCode).then(_=>{
                        console.log(_);
                        me.refreshChannels();
                    });
                }
                catch(er){
                    console.log(er);
                }
            },
            refreshLocalPeers(){
                var me = this;
                try{ 
                    var channelCode = me.channelCode;
                    var index =0;
                    
                    var channel = me.channels.filter(c=>{
                        return c.channelCode == me.channelCode;
                    })[0];
                    var seats = Object.values(channel.seats);
                    var loadItem = ()=>{
                        if( index<me.devices.length){
                            var item = me.devices[index];
                            var peerCode = 'pusher_'+index;
                            var video = document.getElementById('lv-'+index);
                            me.kms.createKmsPeer( peerCode , 's' , video , channelCode, item.deviceId).then(_peer=>{
                                if( _peer && _peer!=null){
                                    me.pushers[peerCode] = _peer;
                                    var seatCode = seats[index].seatCode;
                                    _peer.open( seatCode).then(_=>{
                                        index++;
                                        loadItem();
                                    });
                                }
                            });
                        }
                        else{
                            console.log('load pushers.', me.pushers);
                        }
                    };
                    me.$nextTick().then(_=>{
                        loadItem();
                    });
                }
                catch(er){
                    console.log(er);
                }
            },
            refreshCurrentSeats(){
                var me = this;
                try{ 
                    var channel = me.channels.filter(c=>{
                        return c.channelCode == me.channelCode;
                    })[0];
                    me.currentSeat.seats = Object.values[channel.seats];
                }
                catch(er){
                    console.log(er);
                }
            }
        }
    }
</script>

<style lang="less" scoped>
    ul{
        list-style: none;
        padding: 0;
        margin: 0;
    }
    .divKms{
        height: 100%;
        display: flex;

        .divArea{
            height: 100%;
            width:300px;
            border-right: 2px solid #eee;
            display: flex;
            flex-direction: column;
            .divHead{
                display: flex;
                justify-content: space-between;
                align-items: center;
                border-bottom: 1px solid #ddd;

                .divTitle{
                    padding-left: 10px;
                    border-left: 2px solid #0e067e;
                }

                ul.lstAcs{
                    padding: 10px;
                    display: flex;
                } 
            }
            .divBody{
                flex: 1;
                padding: 10px;
                .divSec{
                    background-color: #fafafa;
                    margin-bottom: 5px;
                    .divSecHead{
                        border-bottom: 1px solid #ddd;
                        display: flex;
                        justify-content: space-between;
                        align-items: center;
                        .divTitle{
                            padding-left: 10px;
                            border-left: 2px solid #0e067e;
                        }
                        ul.lstAcs{
                            padding: 10px;
                            display: flex;
                        } 
                    }
                    .divSecBody{
                        padding: 10px;
                    }
                }
            }
        }

        .divMain{
            flex: 1;
            height: 100%;
            .divRvs{
                height: 100%;
                display: grid;
                grid-template-rows: 33.3333% 33.3333% 33.3333% ;
                grid-template-columns: 33.3333% 33.3333% 33.3333% ;
            }
        }

        ul.lstSeats{
            padding-left: 0px;
            li{
                display: flex;
                border-bottom:1px solid #eee;
                justify-content: space-between;
                align-items: center;
            }
        }

        .divLocalVideo{
            position: relative;
            height: 0;
            padding-bottom: 56.27%;
            video{
                position: absolute;
                width: 100%;
                height: 100%;
                left: 0;
                top:0;
                background-color: #222;
                object-fit: cover;
            }
        }

        .divRemoteVideo{
            position: relative;
            height: 100%;
            width: 100%;
            border: 2px solid #333;
            video{
                position: absolute;
                width: 100%;
                height: 100%;
                left: 0;
                top:0;
                background-color: #222;
                object-fit: cover;
            }
        }
    }
</style>